<?php
/**
 * 非诉订单详情模型
 */
declare(strict_types=1);

namespace app\model\crm;

use app\model\BaseModel;

class LegalServiceOrder extends BaseModel
{
    protected $name = 'crm_legal_service_order';
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    
    // 软删除
    use \think\model\concern\SoftDelete;
    
    // JSON字段
    protected $json = ['service_items', 'special_service_types'];
    
    /**
     * 服务类别常量
     */
    const CATEGORY_PACKAGE = 1;        // 法律服务套餐
    const CATEGORY_SPECIAL = 2;        // 法律专项服务
    const CATEGORY_REGULAR = 3;        // 常规法律服务
    
    /**
     * 套餐类型常量
     */
    const PACKAGE_BASIC = 1;     // 普惠版
    const PACKAGE_STANDARD = 2;  // 标准版
    const PACKAGE_VIP = 3;       // VIP版
    
    /**
     * 账号类型常量
     */
    const ACCOUNT_TYPE_MAIN = 1;  // 主账号
    const ACCOUNT_TYPE_SUB = 2;   // 子账号
    
    /**
     * 关联订单主表
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }
    
    /**
     * 关联主账号客户
     */
    public function mainAccountCustomer()
    {
        return $this->belongsTo(Customer::class, 'main_account_customer_id', 'id');
    }
    
    /**
     * 获取服务类别文本
     */
    public function getServiceCategoryTextAttr($value, $data): string
    {
        $categoryMap = [
            self::CATEGORY_PACKAGE => '法律服务套餐',
            self::CATEGORY_SPECIAL => '法律专项服务',
            self::CATEGORY_REGULAR => '常规法律服务',
        ];
        
        return $categoryMap[$data['service_category']] ?? '未知';
    }
    
    /**
     * 获取套餐类型文本
     */
    public function getPackageTypeTextAttr($value, $data): string
    {
        $packageMap = [
            self::PACKAGE_BASIC    => '普惠版',
            self::PACKAGE_STANDARD => '标准版',
            self::PACKAGE_VIP      => 'VIP版',
        ];
        
        return $packageMap[$data['package_type']] ?? '';
    }
    
    /**
     * 获取账号类型文本
     */
    public function getAccountTypeTextAttr($value, $data): string
    {
        $accountMap = [
            self::ACCOUNT_TYPE_MAIN => '主账号',
            self::ACCOUNT_TYPE_SUB  => '子账号',
        ];
        
        return $accountMap[$data['account_type']] ?? '';
    }
    
    /**
     * 获取剩余诉讼指导配额
     */
    public function getLawsuitGuidanceRemainingAttr($value, $data): int
    {
        return max(0, $data['lawsuit_guidance_quota'] - $data['lawsuit_guidance_used']);
    }
    
    /**
     * 获取剩余商账追收配额
     */
    public function getDebtCollectionRemainingAttr($value, $data): int
    {
        return max(0, $data['debt_collection_quota'] - $data['debt_collection_used']);
    }
    
    /**
     * 检查配额是否充足
     */
    public function hasQuota(string $type): bool
    {
        if ($type === 'lawsuit_guidance') {
            return $this->lawsuit_guidance_quota > $this->lawsuit_guidance_used;
        }
        
        if ($type === 'debt_collection') {
            return $this->debt_collection_quota > $this->debt_collection_used;
        }
        
        return false;
    }
    
    /**
     * 使用配额
     */
    public function useQuota(string $type): bool
    {
        if ($type === 'lawsuit_guidance' && $this->hasQuota($type)) {
            $this->lawsuit_guidance_used += 1;
            return $this->save();
        }
        
        if ($type === 'debt_collection' && $this->hasQuota($type)) {
            $this->debt_collection_used += 1;
            return $this->save();
        }
        
        return false;
    }
}
